package com.ymatou.iapi.exp.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ymatou.iapi.exp.parameter.EOrder;
import com.ymatou.iapi.exp.parameter.GetOrderReceivedCoinListBean;
import com.ymatou.iapi.exp.service.GetOrderReceivedCoinListCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ExpCallService;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*计算订单收货后可获得的金币数
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetOrderReceivedCoinList{
	private static GetOrderReceivedCoinListBean getorderreceivedcoinlistBean;
	private static GetOrderReceivedCoinListCall getorderreceivedcoinlistCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("计算订单收货后可获得的金币数");
	}

	@Before
	public void caseUp() {
		getorderreceivedcoinlistBean=new GetOrderReceivedCoinListBean();
		getorderreceivedcoinlistCall=new GetOrderReceivedCoinListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getorderreceivedcoinlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyData(Integer price,Map pdata) {
		JsonObject result = getorderreceivedcoinlistCall.getJsonObject().get("Data").getAsJsonObject();
		Logger.verifyEquals("true", result.get("success").toString(), "验证success");
		JsonArray ja = result.get("orderCoinList").getAsJsonArray();
		for (JsonElement ordercoin : ja) {
			String orderId=ordercoin.getAsJsonObject().get("orderId").getAsString();
			int rrate=ordercoin.getAsJsonObject().get("rate").getAsInt();
			int rcoin=ordercoin.getAsJsonObject().get("coin").getAsInt();
			int prate=(int)pdata.get(orderId);
			Logger.verifyEquals(prate, rrate, "验证rate");
			int pcoin=price/10*rrate;
			Logger.verifyEquals(pcoin, rcoin, "验证coin");
		}
		
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetOrderReceivedCoinList_001() {
		Logger.start(true,"计算订单收货后可获得的金币数成功,新客非psp1倍");
		try {
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			// urb.setDeviceId(TestDataManager.getRandomUUID().toString());
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer userId = uc.getUserid();
			int sellerid=3383;
			Integer price=TestDataManager.getRandomIntNum(2)+20;
			HashMap pdata=new HashMap<>();
			getorderreceivedcoinlistBean.setUserId(userId);
			List<EOrder> orderList=new ArrayList<>();
			EOrder e=new EOrder();
			e.setIsPspSeller(false);
			e.setSellerId(sellerid);
			e.setOrderId(new Date().getTime());
			e.setOrderTime(YMTDateUtil.getSysTime());
			e.setOrderPrice(price.toString());
			List<String> catalogIds=new ArrayList<>();
			catalogIds.add(TestDataManager.getRandomUUID().toString());
			e.setCatalogIds(catalogIds);
			orderList.add(e);
			//记录参数新客倍率1
			pdata.put(e.getOrderId().toString(), 1);
			getorderreceivedcoinlistBean.setOrderList(orderList);
			getorderreceivedcoinlistCall.setData(getorderreceivedcoinlistBean);
			getorderreceivedcoinlistCall.callService();			
			verifyData(price,pdata);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetOrderReceivedCoinList_002() {
		Logger.start(true,"计算多个订单收货后可获得的金币数成功");
		try {
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			// urb.setDeviceId(TestDataManager.getRandomUUID().toString());
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer userId = uc.getUserid();
			int sellerid=3383;
			int sellerid2=3382;
			Integer price=TestDataManager.getRandomIntNum(2)+20;
			HashMap pdata=new HashMap<>();
			getorderreceivedcoinlistBean.setUserId(userId);
			List<EOrder> orderList=new ArrayList<>();
			EOrder e=new EOrder();
			e.setIsPspSeller(true);
			List<String> catalogIds=new ArrayList<>();
			catalogIds.add(TestDataManager.getRandomUUID().toString());
			e.setCatalogIds(catalogIds);
			e.setSellerId(sellerid);
			e.setOrderId(new Date().getTime());
			e.setOrderTime(YMTDateUtil.getSysTime());
			e.setOrderPrice(price.toString());
			orderList.add(e);
			//记录参数新客倍率1
			pdata.put(e.getOrderId().toString(),ExpCallService.pspRate);
			EOrder e2=new EOrder();
			e2.setIsPspSeller(true);
			List<String> catalogIds2=new ArrayList<>();
			catalogIds.add(TestDataManager.getRandomUUID().toString());
			e2.setCatalogIds(catalogIds);
			e2.setSellerId(sellerid);
			e2.setOrderId(new Date().getTime());
			e2.setOrderTime(YMTDateUtil.getSysTime());
			e2.setOrderPrice(price.toString());
			orderList.add(e2);
			//记录参数新客倍率1
			pdata.put(e2.getOrderId().toString(),ExpCallService.pspRate);
			getorderreceivedcoinlistBean.setOrderList(orderList);
			getorderreceivedcoinlistCall.setData(getorderreceivedcoinlistBean);
			getorderreceivedcoinlistCall.callService();			
			verifyData(price,pdata);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetOrderReceivedCoinList_003() {
		Logger.start(true,"计算多个订单收货后可获得的金币数成功,金币倍率不同");
		try {
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			// urb.setDeviceId(TestDataManager.getRandomUUID().toString());
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer userId = uc.getUserid();
			int sellerid=3383;
			int sellerid2=288484;
			Integer price=TestDataManager.getRandomIntNum(2)+20;
			HashMap pdata=new HashMap<>();
			getorderreceivedcoinlistBean.setUserId(userId);
			List<EOrder> orderList=new ArrayList<>();
			EOrder e=new EOrder();
			e.setIsPspSeller(true);
			List<String> catalogIds=new ArrayList<>();
			catalogIds.add(TestDataManager.getRandomUUID().toString());
			e.setCatalogIds(catalogIds);
			e.setSellerId(sellerid);
			e.setOrderId(new Date().getTime());
			e.setOrderTime(YMTDateUtil.getSysTime());
			e.setOrderPrice(price.toString());
			orderList.add(e);
			//记录参数新客倍率1
			pdata.put(e.getOrderId().toString(),ExpCallService.pspRate);
			Thread.sleep(1000);
			EOrder e2=new EOrder();
			e2.setIsPspSeller(false);
			List<String> catalogIds2=new ArrayList<>();
			catalogIds.add(TestDataManager.getRandomUUID().toString());
			e2.setCatalogIds(catalogIds);
			e2.setSellerId(sellerid2);
			e2.setOrderId(new Date().getTime());
			e2.setOrderTime(YMTDateUtil.getSysTime());
			e2.setOrderPrice(price.toString());
			orderList.add(e2);
			//记录参数新客倍率1
			pdata.put(e2.getOrderId().toString(),1);
			getorderreceivedcoinlistBean.setOrderList(orderList);
			getorderreceivedcoinlistCall.setData(getorderreceivedcoinlistBean);
			getorderreceivedcoinlistCall.callService();			
			verifyData(price,pdata);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetOrderReceivedCoinList_004() {
		Logger.start(true,"不输入userid当做新客处理");
		try {
			Integer userId = 1;
			int sellerid=3383;
			Integer price=TestDataManager.getRandomIntNum(2)+20;
			HashMap pdata=new HashMap<>();
//			getorderreceivedcoinlistBean.setUserId(userId);
			List<EOrder> orderList=new ArrayList<>();
			EOrder e=new EOrder();
			e.setIsPspSeller(false);
			List<String> catalogIds=new ArrayList<>();
			catalogIds.add(TestDataManager.getRandomUUID().toString());
			e.setCatalogIds(catalogIds);
			e.setSellerId(sellerid);
			e.setOrderId(new Date().getTime());
			e.setOrderTime(YMTDateUtil.getSysTime());
			e.setOrderPrice(price.toString());
			orderList.add(e);
			//记录参数新客倍率1
			pdata.put(e.getOrderId().toString(), 1);
			getorderreceivedcoinlistBean.setOrderList(orderList);
			getorderreceivedcoinlistCall.setData(getorderreceivedcoinlistBean);
			getorderreceivedcoinlistCall.callService();	
			JsonObject result = getorderreceivedcoinlistCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			verifyData(price,pdata);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetOrderReceivedCoinList_005() {
		Logger.start(true,"不输入orderList失败");
		try {
			Integer userId = 1;
			int sellerid=3383;
			Integer price=TestDataManager.getRandomIntNum(2)+20;
			HashMap pdata=new HashMap<>();
			getorderreceivedcoinlistBean.setUserId(userId);
			List<EOrder> orderList=new ArrayList<>();
			EOrder e=new EOrder();
			e.setIsPspSeller(true);
			List<String> catalogIds=new ArrayList<>();
			catalogIds.add(TestDataManager.getRandomUUID().toString());
			e.setCatalogIds(catalogIds);
			e.setSellerId(sellerid);
			e.setOrderId(new Date().getTime());
			e.setOrderTime(YMTDateUtil.getSysTime());
			e.setOrderPrice(price.toString());
			orderList.add(e);
			//记录参数新客倍率1
			pdata.put(e.getOrderId().toString(), 5);
//			getorderreceivedcoinlistBean.setOrderList(orderList);
			getorderreceivedcoinlistCall.setData(getorderreceivedcoinlistBean);
			getorderreceivedcoinlistCall.callService();	
			JsonObject result = getorderreceivedcoinlistCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("false", result.get("success").toString(), "验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	//需要配置disconf 活动时间
	public void Tc_GetOrderReceivedCoinList_006() {
		Logger.start(true,"计算订单收货后可获得的金币数成功,新客psp2倍");
		try {
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			// urb.setDeviceId(TestDataManager.getRandomUUID().toString());
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer userId = uc.getUserid();
			int sellerid=3383;
			Integer price=TestDataManager.getRandomIntNum(2)+20;
			HashMap pdata=new HashMap<>();
			getorderreceivedcoinlistBean.setUserId(userId);
			List<EOrder> orderList=new ArrayList<>();
			EOrder e=new EOrder();
			e.setIsPspSeller(true);
			List<String> catalogIds=new ArrayList<>();
			catalogIds.add(TestDataManager.getRandomUUID().toString());
			e.setCatalogIds(catalogIds);
			e.setSellerId(sellerid);
			e.setOrderId(new Date().getTime());
			e.setOrderTime(YMTDateUtil.getSysTime());
			e.setOrderPrice(price.toString());
			orderList.add(e);
			//记录参数新客倍率1
			pdata.put(e.getOrderId().toString(), ExpCallService.pspRate);
			getorderreceivedcoinlistBean.setOrderList(orderList);
			getorderreceivedcoinlistCall.setData(getorderreceivedcoinlistBean);
			getorderreceivedcoinlistCall.callService();			
			verifyData(price,pdata);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	//需要配置数据库 活动时间 20180509 关闭配置
	public void Tc_GetOrderReceivedCoinList_007() {
		Logger.start(true,"计算订单收货后可获得的金币数成功,老客5倍");
		try {
			//老客数据sit使用固定值，以前购买过的：这里数据需要同步到读服务需要时间用存量数据
			Integer userId = ExpCallService.OldUser;
			int sellerid=ExpCallService.OldSeller;
			Integer price=TestDataManager.getRandomIntNum(2)+20;
			HashMap pdata=new HashMap<>();
			getorderreceivedcoinlistBean.setUserId(userId);
			List<EOrder> orderList=new ArrayList<>();
			EOrder e=new EOrder();
			e.setIsPspSeller(true);
			List<String> catalogIds=new ArrayList<>();
			catalogIds.add(TestDataManager.getRandomUUID().toString());
			e.setCatalogIds(catalogIds);
			e.setSellerId(sellerid);
			e.setOrderId(new Date().getTime());
			e.setOrderTime(YMTDateUtil.getSysTime());
			e.setOrderPrice(price.toString());
			orderList.add(e);
			//记录参数新客倍率1
			pdata.put(e.getOrderId().toString(), ExpCallService.oldRate);
			getorderreceivedcoinlistBean.setOrderList(orderList);
			getorderreceivedcoinlistCall.setData(getorderreceivedcoinlistBean);
			getorderreceivedcoinlistCall.callService();			
			verifyData(price,pdata);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}